package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
import java.util.Random;
//model layer package has to be imported
/**
 * Controller class for the DVD collection
 * 
 * @author (team 1)
 * DVD controller for creating copies and dvds
 */
public class DVDCtr
{
    //declare instance variables to reference containers
    private DVDContainer dvdCollection;

    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()
    {
        dvdCollection = DVDContainer.getInstance();
    }

    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    /**
     * returns dvd
     * @param long id
     * @return DVD(id)
       */
    public DVD findDVD(long id)
    {
        //this method is to search the container for a DVD
        //with an id-number equal to the parameter no
        //when the dvd is found, the method is to call print-methods in the TUI layer
        return dvdCollection.getDVD(id);
    }
    /**
     * Creates a DVD
     * @param String title, String artist, String publicationDate
       */
    public void createDVD(String title, String artist, String publicationDate)
    {       
        //this method constructs a new dvd object 
        //and stores it in the container    
        boolean free = false;
        int newID = 0;
        DVD tempDvd = null;
        while(!free)
        {
            Random rand = new Random(); 
            int value = rand.nextInt(5000);
            newID = value;

            tempDvd = findDVD(newID);

            if(tempDvd == null)
            {
                free = true;
            }
            else
            {
                free = false;
            }
        }

        if(free)
        {
            DVD d = new DVD(newID, title, artist, publicationDate);
            dvdCollection.addDVD(d);
        }

    }
    /**
     * Deletes a dvd
     * @param long id
       */
    public void deleteDVD(long id)
    {

        //search for the dvd and delete the object from the container
        dvdCollection.deleteDVD(id);
    }
    /**
     * Lists all dvds
       */
    public void listAllDvds()
    {
        //returns a list of all dvds and their associated copies
        dvdCollection.listAllDvd();
    }

    /**
     * All the copy related stuff can be found here
     * Copy Ctr for CRUD of Copy (Minus update)
     */
    /**
     * Lists all copies
       */
    public void listAllCopies()
    {
        //returns a list of all dvds and their associated copies
        dvdCollection.listAllCopy();
    }
    /**
     * Create a copy
     * @param long id, String date, String price
       */
    public void createCopy(long id, String date, String price)
    {
        Random rand = new Random(); 
        int value = rand.nextInt(50);

        long sn = id + value;

        Copy copy = new Copy(id, sn, date, price);
        dvdCollection.addCopy(copy);
    }
    /**
     * Deletes a copy
     * @param long sn
       */
    public void deleteCopy(long sn)
    {
        dvdCollection.deleteCopy(sn);
    }
    /**
     * returns a copy
     * @param long sn
       */
    public Copy findCopy(long sn)
    {
        return dvdCollection.getCopy(sn);
    }
}
